package com.atxm.mimall.product.service.impl;

import com.atxm.mimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atxm.common.utils.PageUtils;
import com.atxm.common.utils.Query;

import com.atxm.mimall.product.dao.BrandDao;
import com.atxm.mimall.product.entity.BrandEntity;
import com.atxm.mimall.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //构建查询条件
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        //获取品牌ID、品牌名、检索首字母：如果不为空,加入查询条件
        if (!StringUtils.isEmpty(params.get("brandId"))){
            queryWrapper.eq("brand_id",params.get("brandId"));
        }
        if (!StringUtils.isEmpty(params.get("brandName"))){
            queryWrapper.like("name",params.get("brandName"));
        }
        if (!StringUtils.isEmpty(params.get("firstletter"))){
            queryWrapper.like("first_letter",params.get("firstletter"));
        }

        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), queryWrapper);

        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        //保证冗余字段的数据一致
        this.updateById(brand);
        if(!StringUtils.isEmpty(brand.getName())){
            //同步更新其他关联表中的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

            //TODO 更新其他关联
        }    }

}